/*
    <one line to give the program's name and a brief idea of what it does.>
    Copyright (C) <year>  <name of author>

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License along
    with this program; if not, write to the Free Software Foundation, Inc.,
    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

*/

#include "subscene.h"
#include <math.h>
using namespace scene;

SubScene::~SubScene(){
  if(m_transformation != NULL)
    delete m_transformation;
}
    
void SubScene::setTransFormation(Vertex * temp, float rotate)
{
  switch(m_type){
    case TRANSLATE: setupTranslation(temp);
      break;
    case ROTATE: setupRotation(temp,rotate);
      break;
    case SCALE: setupScaling(temp);
      break;
  }
}

void SubScene::setupTranslation(Vertex * temp)
{
  m_transformation = new draw::FloatMatrix();
  m_transformation->setIdentity();
  m_transformation->seta3(temp->x());
  m_transformation->setb3(temp->y());
  m_transformation->setc3(temp->z());
}

void SubScene::setupRotation(Vertex * temp, float rotate)
{
  temp->setH(0);
  temp->normalize();
  m_transformation = new draw::FloatMatrix();
  m_transformation->setIdentity();
  temp->setH(0);
  temp->normalize();
  
  float c = cos (rotate*3.14159265/(float)180);
  float s = sin (rotate*3.14159265/(float)180);
  
  float x = temp->x();
  float y = temp->y();
  float z = temp->z();
  
  float xQ = temp->x()*temp->x();
  float yQ = temp->y()*temp->y();
  float zQ = temp->z()*temp->z();
  
  m_transformation->seta0(xQ + (1.0- xQ)*c)  ;
  m_transformation->seta1(x*y*(1.0-c) - z*s)  ;
  m_transformation->seta2(x*z*(1.0-c) + y*s)  ;
  m_transformation->setb0(x*y*(1.0-c) + z*s)  ;
  m_transformation->setb1(yQ + (1.0- yQ)*c) ;
  m_transformation->setb2(y*z*(1.0-c) - x*s)  ;
  m_transformation->setc0(x*z*(1.0-c) - y*s)  ;
  m_transformation->setc1(y*z*(1.0-c) + x*s)  ;
  m_transformation->setc2(zQ + (1.0- zQ)*c) ;
}

void SubScene::setupScaling(Vertex * temp)
{
  m_transformation = new draw::FloatMatrix();
  
  m_transformation->seta0(temp->x());
  m_transformation->setb1(temp->y());
  m_transformation->setc2(temp->z());
  m_transformation->setd3(1);
  
}